import {
  Component, OnInit, ViewEncapsulation, ViewChild, AfterViewInit,
  ElementRef, OnDestroy
} from '@angular/core';
import { SeedInfiniteScroll } from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import { AuthService } from "@shared/service/auth/auth.service";
import { Content } from "@shared/components/toolbar/toolbar-content";
import { ToolsService } from "@shared/service/tools/tools.service";
import { HaggleService } from "../haggle.service";
import { Subject } from "rxjs/Rx";
// import {HaggleInitiatorRespVO} from "@interface/index";


@Component({
  selector: 'seed-haggle-list',
  templateUrl: './haggle-list.component.html',
  styleUrls: ['./haggle-list.component.scss'],
  encapsulation: ViewEncapsulation.None,
})

export class HaggleListComponent implements OnInit, OnDestroy {

  public statusList: any = [
    {name: '我发起的', join: 1},
    {name: '我参与的', join: 2}
  ];
  public myHaggle: any;
  public param: any = {
    join: 1,
    page: 1,
    rows: 10
  };

  public isActive = 0;
  public showImg = true;
  public haggleList: any = []; // Array<HaggleInitiatorRespVO>
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public hasData = true ;

  @ViewChild('orderNav') orderNav: ElementRef;
  @ViewChild(Content) content;
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(public haggleService: HaggleService,

  ) {
  }
  /**
   * @description 头部nav切换
   * @author @chensk740
   * @date 201780305
   * @param idx nav序号
   */
  ChangeStatus(idx) {
    this.hasData = true;
    this.myHaggle = this.statusList[idx].name;
    this.isActive = idx;
    this.param['page'] = 1;
    this.param['join'] = this.statusList[idx].join;
    this.haggleList = [];
    this.getMyHaggleList();
    if (this.infiniteScroll) {
      this.infiniteScroll.enable(true);
    }
  }

  /**
   * @description 我的砍价列表 join-我参与的类型(1:我发起的砍价，2:我参与的砍价)
   * @author @chensk740
   * @date 201780305
   */
  getMyHaggleList() {
    this.haggleService.getMyHaggleList(this.param)
      .takeUntil(this.ngUnsubscribe)
      .subscribe(res => {
        if (res.result === '0' && this.param.page === 1 && res.data && res.data.list.length === 0) {
         this.hasData = false ;
        }
        if (res.result === '0' && res.data && res.data.list.length > 0) {
          this.param['page']++;
          this.infiniteScroll.complete();
          if (res.data.hasNextPage === false) {
            this.infiniteScroll.enable(false);
          }
          this.haggleList = [...this.haggleList, ...res.data.list];
        }
      });
  }
  ngOnInit() {
    this.haggleList = [];
    this.getMyHaggleList();
  }

  ngOnDestroy() {
    this.ngUnsubscribe.unsubscribe();
  }

}

